package com.cn.kabalifeuser.ui.integral.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alipay.sdk.app.PayTask;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.callback.SchedulerTransformer;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.event.RxBus;
import com.cn.kabalifeuser.event.RxBusBean;
import com.cn.kabalifeuser.service.Logger;
import com.cn.kabalifeuser.ui.integral.adapter.CommodityCouponFillAdapter;
import com.cn.kabalifeuser.ui.integral.bean.CustRemarksBean;
import com.cn.kabalifeuser.ui.integral.bean.DefaultAddressBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderCarBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderCouponsBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderPlaceBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderSetBean;
import com.cn.kabalifeuser.ui.integral.bean.WxPayBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.activity.IAddressListActivity;
import com.cn.kabalifeuser.ui.my.activity.OrderListAcivity;
import com.cn.kabalifeuser.ui.release.bean.PayResult;
import com.cn.kabalifeuser.ui.release.bean.ZfbBean;
import com.cn.kabalifeuser.utils.GlideUtil;
import com.cn.kabalifeuser.utils.GsonUtil;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.utils.SoftHideKeyBoardUtil;
import com.cn.kabalifeuser.utils.StringUtil;
import com.cn.kabalifeuser.view.PopupWindow.CommonPopupWindow;
import com.cn.kabalifeuser.view.PopupWindow.CommonUtil;
import com.cn.kabalifeuser.view.dialogView.PayErrDialog;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by 晓林 on 2020/9/18.
 * function: 确认订单
 */
public class OrderConfirmActivity extends IBaseActivity<OrderConfirmView, OrderConfirmPresenter> implements OrderConfirmView, CommonPopupWindow.ViewInterface {
    public static final int onActivityResult_To_Address = 110;

    @BindView(R.id.tv_btn_address)
    TextView tvBtnAddress;
    @BindView(R.id.tv_address_name)
    TextView tvAddressName;
    @BindView(R.id.tv_address_phone)
    TextView tvAddressPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.ll_btn_address)
    LinearLayout llBtnAddress;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_money_all)
    TextView tvMoneyAll;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;

    int CouponID = 0;
    int addressId = 0;


    Bundle bundle;
    String s_moneyAll, s_num;
    List<OrderSetBean> mList;
    String mIsGoods;//商品的id
    String mIsCards;//购物车id
    int sign;


    OrderConfirmAdapter mAdapter;

    int paySign = 1;

    double amount = 0;

    int orderID = -1;//返回的订单id

    private CommonPopupWindow popupWindowPay, popupWindowCoupon;
    List<OrderCouponsBean.ResultBean> mListCoupons = new ArrayList<>(); //优惠券


    boolean isSign = true;// 标识 进来请求  还是点击优惠券请求

    @Override
    protected OrderConfirmPresenter createPresenter() {
        return new OrderConfirmPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_order_confirm;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        SoftHideKeyBoardUtil.assistActivity(this);
        setTitleBar("确认订单");
        tvBtnAddress.setVisibility(View.VISIBLE);
        llBtnAddress.setVisibility(View.GONE);
        bundle = getIntent().getExtras();
        s_moneyAll = bundle.getString("s_moneyAll");
        s_num = bundle.getString("s_num");
        mList = (List<OrderSetBean>) bundle.getSerializable("ListOrderSetBeans");
        for (int i = 0; i < mList.size(); i++) {
            LoggerUtils.e("打印："+mList.toString());
        }
        List<String> strId = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            strId.add(String.valueOf(mList.get(i).getId()));
        }
        mIsGoods = StringUtil.convertListToString(strId);
        sign = bundle.getInt("sign");
        if (sign == 1)
            // String good_id, String type, String id, String good_num, String spec_id
            mPresenter.onCouponsData(LoginHelper.getLonginData().getResult(), String.valueOf(mList.get(0).getId()), "buy_now", "", String.valueOf(mList.get(0).getNum()), String.valueOf(mList.get(0).getSpecID()));
        else {
            mIsCards = bundle.getString("mIsGoods");
            mPresenter.onCouponsData(LoginHelper.getLonginData().getResult(), "", "cart", mIsGoods, "", "");
        }

        //默认地址
        mPresenter.onAddressData(LoginHelper.getLonginData().getResult());

        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rvData.setLayoutManager(layoutManager);
        rvData.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new OrderConfirmAdapter(R.layout.order_confirm_item, mList);
        //给RecyclerView设置适配器
        rvData.setAdapter(mAdapter);


        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean.class)
                .compose(new SchedulerTransformer<RxBusBean>())
                .subscribe(new Action1<RxBusBean>() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void call(RxBusBean rxBusBean) {
                        if (rxBusBean.getId() == 2001 && rxBusBean.getSign() == 2001) {
                            RxToast.success("支付成功");
                            Constants.PAY_SIGN = 0;
                            finish();
                        }
                    }
                });

    }

    @OnClick({R.id.tv_btn_address, R.id.ll_btn_address, R.id.tv_btn_next, R.id.ll_btn_coupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_btn_coupon:
                //todo 优惠
                isSign = false;
                if (sign == 1)
                    // String good_id, String type, String id, String good_num, String spec_id
                    mPresenter.onCouponsData(LoginHelper.getLonginData().getResult(), String.valueOf(mList.get(0).getId()), "buy_now", "", String.valueOf(mList.get(0).getNum()), String.valueOf(mList.get(0).getSpecID()));
                else {
                    mIsCards = bundle.getString("mIsGoods");
                    mPresenter.onCouponsData(LoginHelper.getLonginData().getResult(), "", "cart", mIsGoods, "", "");
                }
                break;
            case R.id.tv_btn_address:
                Intent intent = new Intent(getMe(), IAddressListActivity.class);
                intent.putExtra("sign", onActivityResult_To_Address);
                startActivityForResult(intent, onActivityResult_To_Address);
                break;
            case R.id.ll_btn_address:
                intent = new Intent(getMe(), IAddressListActivity.class);
                intent.putExtra("sign", onActivityResult_To_Address);
                startActivityForResult(intent, onActivityResult_To_Address);
                break;
            case R.id.tv_btn_next:
//                , String type, int good_id, int spec_id, int good_num, int pay_type, int address_id, String content, String coupon_id
                if (addressId > 0) {
                    if (sign == 1) {
                        if (orderID > 0) {
                            showPay();
                        } else {
                            List<CustRemarksBean> list = new ArrayList<>();
                            if (!TextUtils.isEmpty(mList.get(0).getRemark()))
                                list.add(new CustRemarksBean(mList.get(0).getSpecID(), mList.get(0).getRemark()));

                            LoggerUtils.e("返回" + GsonUtil.getJsonData(list));
                            mPresenter.onOrderPayData(LoginHelper.getLonginData().getResult(), "buy_now",
                                    mList.get(0).getId(), mList.get(0).getSpecID(), mList.get(0).getNum(), addressId, GsonUtil.getJsonData(list), CouponID, LoginHelper.getCityData().getResult().getId()
                            );
                        }

                    } else {
                        if (orderID > 0) {
                            showPay();
                        } else {
                            //todo 购物车
                            List<CustRemarksBean> list = new ArrayList<>();
                            for (int i = 0; i < mList.size(); i++) {
                                if (!TextUtils.isEmpty(mList.get(i).getRemark()))
                                    list.add(new CustRemarksBean(mList.get(0).getSpecID(), mList.get(0).getRemark()));
                            }
                            LoggerUtils.e("返回" + GsonUtil.getJsonData(list));
                            mPresenter.onOrderPayData(LoginHelper.getLonginData().getResult(), "cart", mIsCards,
                                    addressId, GsonUtil.getJsonData(list), CouponID, LoginHelper.getCityData().getResult().getId()
                            );
                        }
                    }
                } else RxToast.error("请选择您的收货地址");
                break;
        }
    }

    /**
     * 优惠劵
     */
    private void showCoupon() {
        if (popupWindowCoupon != null && popupWindowCoupon.isShowing()) return;
        View upView = LayoutInflater.from(getMe()).inflate(R.layout.pw_commodity_coupon_, null);
        //测量View的宽高
        CommonUtil.measureWidthAndHeight(upView);
        popupWindowCoupon = new CommonPopupWindow.Builder(getMe())
                .setView(R.layout.pw_commodity_coupon_)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                .setAnimationStyle(R.style.AnimUp)
                .setViewOnclickListener(this)
                .create();
        popupWindowCoupon.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }


    /**
     * 支付
     */
    private void showPay() {
        if (popupWindowPay != null && popupWindowPay.isShowing()) return;
        View upView = LayoutInflater.from(getMe()).inflate(R.layout.pw_commodity_pay, null);
        //测量View的宽高
        CommonUtil.measureWidthAndHeight(upView);
        popupWindowPay = new CommonPopupWindow.Builder(getMe())
                .setView(R.layout.pw_commodity_pay)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                .setAnimationStyle(R.style.AnimUp)
                .setViewOnclickListener(this)
                .setOutsideTouchable(false)
                .create();
        popupWindowPay.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }


    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.pw_commodity_coupon_:
                RecyclerView rvCoupon = view.findViewById(R.id.recyclerView);
                rvCoupon.setLayoutManager(new LinearLayoutManager(getMe()));
                //创建适配器
                CommodityCouponFillAdapter commodityCouponAdapter = new CommodityCouponFillAdapter(R.layout.pw_commodity_coupon_fill_item, mListCoupons);
                //给RecyclerView设置适配器
                rvCoupon.setAdapter(commodityCouponAdapter);
                commodityCouponAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        CouponID = commodityCouponAdapter.getItem(position).getId();
                        tvCoupon.setText(commodityCouponAdapter.getItem(position).getTitle());
                        if (sign == 2) {
                            mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                                    "cart", mIsGoods, CouponID);
                        } else {
                            mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                                    "buy_now", mList.get(0).getId(), mList.get(0).getSpecID(), mList.get(0).getNum(), CouponID);
                        }


                    }
                });
                view.findViewById(R.id.tv_btn_colce).

                        setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                popupWindowCoupon.dismiss();
                            }
                        });
                view.findViewById(R.id.tv_spit).

                        setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                CouponID = 0;
                                tvCoupon.setText("不使用优惠券");
                                if (sign == 2) {
                                    mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                                            "cart", mIsGoods, CouponID);
                                } else {
                                    mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                                            "buy_now", mList.get(0).getId(), mList.get(0).getSpecID(), mList.get(0).getNum(), CouponID);
                                }

                            }
                        });
                break;
            //todo 支付
            case R.layout.pw_commodity_pay:
                ImageView img_check_weixin = view.findViewById(R.id.img_check_weixin);
                ImageView img_check_zfb = view.findViewById(R.id.img_check_zfb);
                img_check_weixin.setImageResource(R.drawable.check_true);
                img_check_zfb.setImageResource(R.drawable.check_false);
                paySign = 1;

                view.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Constants.PAY_SIGN = 0;
                        if (popupWindowPay != null)
                            popupWindowPay.dismiss();
                        bundle = new Bundle();
                        bundle.putInt("sign", 2);
                        $startActivityFinish(OrderListAcivity.class, bundle);
                    }
                });
                view.findViewById(R.id.ll_btn_pay_zfb).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        img_check_weixin.setImageResource(R.drawable.check_false);
                        img_check_zfb.setImageResource(R.drawable.check_true);
                        paySign = 2;
                    }
                });
                view.findViewById(R.id.ll_btn_pay_weixin).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        img_check_weixin.setImageResource(R.drawable.check_true);
                        img_check_zfb.setImageResource(R.drawable.check_false);
                        paySign = 1;
                    }
                });
                view.findViewById(R.id.ll_btn_pay_qianbao).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PayErrDialog   payErrDialog=new PayErrDialog(getMe());
                        payErrDialog.show();
                    }
                });
                view.findViewById(R.id.ll_btn_pay_yinlian).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PayErrDialog payErrDialog=new PayErrDialog(getMe());
                        payErrDialog.show();
                    }
                });
                view.findViewById(R.id.tv_btn_pay).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //todo 调用支付接口

                        //todo
                        LoggerUtils.e("积分判断：" + amount);
                        if (amount == 0)
                            mPresenter.onOrderPayGoData(LoginHelper.getLonginData().getResult(), orderID, 3);
                        else
                            mPresenter.onOrderPayGoData(LoginHelper.getLonginData().getResult(), orderID, paySign);
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case onActivityResult_To_Address:
                //上门返回来的地址。
                if (data != null) {
                    addressId = data.getIntExtra("addressId", 0);
                    String addressName = data.getStringExtra("addressName");
                    String address = data.getStringExtra("address");
                    String addressPhone = data.getStringExtra("addressPhone");
                    if (!TextUtils.isEmpty(addressName) && addressId > 0) {
                        tvBtnAddress.setVisibility(View.GONE);
                        llBtnAddress.setVisibility(View.VISIBLE);
                    } else {
                        tvBtnAddress.setVisibility(View.VISIBLE);
                        llBtnAddress.setVisibility(View.GONE);
                    }
                    tvAddressName.setText(addressName);
                    tvAddressPhone.setText(addressPhone);
                    tvAddress.setText(address);
                }
                break;
//            case onActivityResult_To_Invoice:
//                if (data != null) {
//                    invoice = data.getIntExtra("invoice", 0);
//                    if (invoice == 0)
//                        tvInvoice.setText("不开发票");
//                    else tvInvoice.setText("已开发票");
//
//                }
//                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onOrderPaySuccess(String json) {
        if (amount == 0) {
            LoggerUtils.e("积分购买：" + json);
            MsgBean bean = GsonUtil.GsonToJsonClass(json, MsgBean.class);
            if (bean.getCode() == Constants.HTTP_SUCCESS) {
                RxToast.success(bean.getMsg());
                finish();
            } else if (bean.getCode() == Constants.HTTP_AGAIN_LOGIN) {
                onReLoggedIn(bean.getMsg());
            } else onError(bean.getMsg());
        } else {
            if (paySign == 2) {
                //todo 支付宝
                ZfbBean bean = GsonUtil.GsonToJsonClass(json, ZfbBean.class);
                if (bean.getCode() == Constants.HTTP_SUCCESS) {
                    //TODO 支付宝支付
                    final String orderInfo = bean.getResult();
                    Runnable payRunnable = new Runnable() {
                        @Override
                        public void run() {
                            PayTask alipay = new PayTask(
                                    OrderConfirmActivity.this);
                            Map<String, String> result = alipay
                                    .payV2(orderInfo, true);
                            LoggerUtils.d("支付宝数据:" + result.toString());
                            Message msg = new Message();
                            msg.what = SDK_PAY_FLAG;
                            msg.obj = result;
                            mHandler.sendMessage(msg);
                        }
                    };

                    Thread payThread = new Thread(payRunnable);
                    payThread.start();
                } else if (bean.getCode() == Constants.HTTP_AGAIN_LOGIN) {
                    onReLoggedIn(bean.getMsg());
                } else onError(bean.getMsg());
            } else if (paySign == 1) {
                //todo 微信
                WxPayBean bean = GsonUtil.GsonToJsonClass(json, WxPayBean.class);
                if (bean.getCode() == Constants.HTTP_SUCCESS) {
                    Constants.PAY_SIGN = 2001;
                    IWXAPI api = WXAPIFactory.createWXAPI(getMe(), bean.getResult().getAppid());
                    api.registerApp(bean.getResult().getAppid());
                    PayReq req = new PayReq();
                    req.appId = bean.getResult().getAppid();
                    req.partnerId = bean.getResult().getPartnerid();  //商户id
                    req.prepayId = bean.getResult().getPrepayid(); //预支付交易会话ID
                    req.packageValue = bean.getResult().getPackageX(); //扩展字段
                    req.nonceStr = bean.getResult().getNoncestr(); //随机字符串
                    req.timeStamp = String.valueOf(bean.getResult().getTimestamp());//时间戳
                    req.sign = bean.getResult().getSign(); //签名
                    api.sendReq(req);
                } else if (bean.getCode() == Constants.HTTP_AGAIN_LOGIN) {
                    onReLoggedIn(bean.getMsg());
                } else onError(bean.getMsg());

            }
        }

    }

    @Override
    public void onOrderCarSuccess(OrderCarBean bean) {
        orderID = bean.getResult().getOrder_id();
        showPay();
    }

    @Override
    public void onCouponsSuccess(OrderCouponsBean bean) {
        if (bean.getResult() != null && bean.getResult().size() > 0) {
            if (isSign) {
                CouponID = bean.getResult().get(0).getId();
                tvCoupon.setText(bean.getResult().get(0).getTitle());
                if (sign == 2) {
                    mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                            "cart", mIsCards, bean.getResult().get(0).getId());
                } else {
                    mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                            "buy_now", mList.get(0).getId(), mList.get(0).getSpecID(), mList.get(0).getNum(), bean.getResult().get(0).getId());
                }
            } else {
                mListCoupons.clear();
                mListCoupons = bean.getResult();
                showCoupon();
            }

        } else {
            tvNum.setText(s_num);
            tvMoneyAll.setText(s_moneyAll);
            tvCoupon.setText("暂无优惠券");
            if (isSign) {
                if (sign == 2) {
                    mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                            "cart", mIsCards, 0);
                } else {
                    mPresenter.onOrderData(LoginHelper.getLonginData().getResult(),
                            "buy_now", mList.get(0).getId(), mList.get(0).getSpecID(), mList.get(0).getNum(),0);
                }
            } else {

            }
        }
    }

    @Override
    public void onOrderSuccess(OrderPlaceBean bean) {
        //显示总价
        amount = bean.getResult().getAmount();
        LoggerUtils.e("积分：" + bean.getResult().getAmount());
        if (popupWindowCoupon != null)
            popupWindowCoupon.dismiss();
        tvNum.setText(bean.getResult().getGood_num());
        tvMoneyAll.setText(bean.getResult().getTotal_amount());
    }

    @Override
    public void onAddressSuccess(DefaultAddressBean bean) {
        if (bean.getResult() != null && bean.getResult().getId() > 0) {
            addressId = bean.getResult().getId();
            tvAddressName.setText(bean.getResult().getName());
            tvAddressPhone.setText(bean.getResult().getPhone());
            tvAddress.setText(bean.getResult().getAddress());
            tvBtnAddress.setVisibility(View.GONE);
            llBtnAddress.setVisibility(View.VISIBLE);
        } else {
            tvBtnAddress.setVisibility(View.VISIBLE);
            llBtnAddress.setVisibility(View.GONE);
        }
    }

    private static final int SDK_PAY_FLAG = 1;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult(
                            (Map<String, String>) msg.obj);
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
//                        LoggerUtils.d("支付宝支付成功咯~~~");
                        RxToast.success("支付成功");
                        finish();
//                        Bundle bundle = new Bundle();
//                        bundle.putString("money", pay_amount);
//                        bundle.putInt("signPay", signPay);
//                        bundle.putInt("sign", sign);
//                        $startActivityFinish(OrdeFillingPaySuccessActivity.class, bundle);
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
//                        LoggerUtils.d("支付宝支付失敗了~~~");
//                        RxToast.error("支付失败!");
                        bundle = new Bundle();
                        bundle.putInt("sign", 2);
                        $startActivityFinish(OrderListAcivity.class, bundle);

                    }
                    break;
                }
                default:
                    break;
            }
        }


    };

    class OrderConfirmAdapter extends BaseQuickAdapter<OrderSetBean, BaseViewHolder> {


        public OrderConfirmAdapter(int layoutResId, @Nullable List<OrderSetBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final OrderSetBean item) {
            GlideUtil.ImageLoad(this.mContext, item.getImg(), helper.getView(R.id.commodity_img));
            helper.setText(R.id.commodity_title, item.getTitle())
                    .setText(R.id.tv_check_spec, item.getSpec())
                    .setText(R.id.commodity_price, item.getMoney())
                    .setText(R.id.tv_num, "x " + item.getNum())
            ;

            EditText et_remarks = helper.getView(R.id.et_remarks);
            et_remarks.setText(item.getRemark());
            et_remarks.addTextChangedListener(new MyEdWatcher(helper.getPosition()));

            TextView tv_postage = helper.getView(R.id.tv_postage);
            double d = Double.parseDouble(item.getPostage());
            if (d > 0)
                tv_postage.setText(String.valueOf(d));
            else tv_postage.setText("免邮");

        }

    }


    class MyEdWatcher implements TextWatcher {

        int index;

        public MyEdWatcher(int index) {
            this.index = index;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            mList.get(index).setRemark(s.toString());
        }
    }


}
